package com.enjoy.list;

public class DoubleLinkedList {//双向链表

    private Node head;

    private Node tail;

    DoubleLinkedList() {
    }

//    public void insertHead(char value) {
//        Node newNode = new Node(value);
//        if (head == null) {
//            tail = newNode;
//        } else {
//            head.pre = newNode;
//            newNode.next = head;
//        }
//        head = newNode;
//    }

    public void insertTail(char value) {
        Node newNode = new Node(value);
        if (tail == null) {
            head = newNode;
        } else {
            tail.next = newNode;
            newNode.pre = tail;
        }
        tail = newNode;
    }

    private static class Node {
        char value;

        Node next;

        Node pre;

        Node(char value) {
            this.value = value;
        }
    }

    private void print(){
        Node node = head;
        while (node!=null) {
            System.out.println(node.value);
            node = node.next;
        }
        System.out.println(tail.value);
    }

    public static void main(String[] args) {

        DoubleLinkedList linkedList = new DoubleLinkedList();
        linkedList.insertTail('a');
        linkedList.insertTail('b');
        linkedList.insertTail('c');
        linkedList.insertTail('d');

        linkedList.print();
    }
}
